<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 * @version    $Id: Controller.php 2011-03-09 08:45:41Z idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Kowcore
 * @copyright  Copyright KEENONWEB LLC
 * @license    http://www.keenonweb.com/
 */

class Kowcore_Widget_BestCommentersController extends Engine_Content_Widget_Abstract
{
  public function indexAction()
  {
    $commentTable = Engine_Api::_()->getDbTable('comments', 'core');
    $userTable = Engine_Api::_()->getItemTable('user');
    $name = $userTable->info('name');
    $commentName = $commentTable->info('name');
    $viewer = Engine_Api::_()->user()->getViewer();

    $select = $userTable->select()
      ->setIntegrityCheck(false)
      ->from($name)
      ->joinLeft($commentName, $commentName.'.poster_id = '.$name.'.user_id', array('left_comments' => 'COUNT('.$commentName.'.comment_id)'))
      ->order('left_comments DESC')
      ->group($name.'.user_id');

    $this->view->paginator = $paginator = Zend_Paginator::factory($select);
    $paginator->setItemCountPerPage(9);
    
    if (!$paginator->getTotalItemCount()) {
      return $this->setNoRender();
    }
  }
}